/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.graphics.Rect;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.ouertech.android.agm.lib.base.utils.UtilMD5;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/4/1.
 * desc    : 修改密码界面
 */
public class UserModifyPwdActivity extends BaseTopActivity{
    //旧密码
    @Bind(R.id.pwd_id_oldpwd)
    EditText mEtOldPasswd;
    //密码
    @Bind(R.id.pwd_id_pwd)
    EditText mEtPasswd;
    //确认密码
    @Bind(R.id.pwd_id_confirm_pwd)
    EditText mEtConfirmPwd;
    //完成
    @Bind(R.id.pwd_id_finish)
    Button mBtnFinish;

    @Override
    protected void initTop() {
        setTitle(R.string.user_modify_pwd);
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_user_modify_pwd);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        final int width = UtilDevice.getDevice(this).getWidth();
        final int height = (int)(56 * UtilDevice.getDevice(this).getDensity());

        mBtnFinish.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        Rect rect = new Rect();
                        mBtnFinish.getWindowVisibleDisplayFrame(rect);
                        mBtnFinish.layout(0,
                                rect.bottom - rect.top - 2 * height,
                                width,
                                rect.bottom - rect.top - height);
                    }
                });
    }


    @OnClick(R.id.pwd_id_finish)
    public void next() {
        modifyPwd();
    }


    /**
     * 修改密码
     */
    private void modifyPwd() {
        String oldPwd = mEtOldPasswd.getText().toString().trim();
        String passwd = mEtPasswd.getText().toString().trim();
        String confirmPwd = mEtConfirmPwd.getText().toString().trim();

        //密码为空
        if(UtilString.isBlank(oldPwd)) {
            UtilOuer.toast(this, R.string.user_modify_pwd_old_empty);
            mEtOldPasswd.requestFocus();
            return;
        }

        //密码格式错误
        if(passwd.length() <6 || passwd.length()>16) {
            UtilOuer.toast(this, R.string.user_modify_pwd_old_error);
            mEtOldPasswd.requestFocus();
            return;
        }

        //密码为空
        if(UtilString.isBlank(passwd)) {
            UtilOuer.toast(this, R.string.user_modify_pwd_new_empty);
            mEtPasswd.requestFocus();
            return;
        }

        //密码格式错误
        if(passwd.length() <6 || passwd.length()>16) {
            UtilOuer.toast(this, R.string.user_modify_pwd_new_error);
            mEtPasswd.requestFocus();
            return;
        }

        //确认密码为空
        if(UtilString.isBlank(confirmPwd)) {
            UtilOuer.toast(this, R.string.user_modify_pwd_confirm_empty);
            mEtConfirmPwd.requestFocus();
            return;
        }

        //两次密码不一致
        if(!passwd.equals(confirmPwd)) {
            UtilOuer.toast(this, R.string.user_modify_pwd_confirm_error);
            mEtConfirmPwd.requestFocus();
            return;
        }


        //忘记密码
        AgnettyFuture future = OuerApplication.mOuerFuture.modifyPwd(
                UtilMD5.getStringMD5(oldPwd),UtilMD5.getStringMD5(passwd),
                new OuerFutureListener(this) {

                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);
                        UtilOuer.toast(UserModifyPwdActivity.this, R.string.user_forget_pwd_success);
                        finish();
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        UtilOuer.showExceptionToast(UserModifyPwdActivity.this, result,
                                R.string.user_modify_pwd_failure);
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }
                });

        attachDestroyFutures(future);
    }

}
